package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.returnedpurchaseproduct.pojo.dto.ReturnedPurchaseProductDeleteBatchDTO;
import com.xbongbong.pro.returnedpurchaseproduct.pojo.vo.ReturnedPurchaseProductDeleteBatchVO;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseProductEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.ReturnedPurchaseProductModel;
import com.xbongbong.saas.service.ProductSerialService;
import com.xbongbong.saas.service.ReturnedPurchaseProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/3/8 14:14
 * @since v1.0
 */
@Service("returnedPurchaseProductService")
public class ReturnedPurchaseProductServiceImpl implements ReturnedPurchaseProductService {

    private static final Logger LOG = LoggerFactory.getLogger(ReturnedPurchaseProductServiceImpl.class);

    @Resource
    private ReturnedPurchaseProductModel returnedPurchaseProductModel;
    @Resource
    private ProductSerialService productSerialService;

    @Override
    public ReturnedPurchaseProductDeleteBatchVO deleteBatch(ReturnedPurchaseProductDeleteBatchDTO returnedPurchaseProductDeleteBatchDTO) throws XbbException {
        ReturnedPurchaseProductDeleteBatchVO returnedPurchaseProductDeleteBatchVO = new ReturnedPurchaseProductDeleteBatchVO();
        try {
            List<Long> returnedPurchaseIdIn = returnedPurchaseProductDeleteBatchDTO.getReturnedPurchaseIdIn();
            String corpid = returnedPurchaseProductDeleteBatchDTO.getCorpid();
            Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid",corpid);
            param.put("del",0);
            param.put("returnPurchaseIdIn",returnedPurchaseIdIn);
            List<ReturnedPurchaseProductEntity> list = returnedPurchaseProductModel.findEntitys(param);
            returnedPurchaseProductDeleteBatchVO.setReturnedPurchaseProductList(list);
            // 删除采购退货单序列号流水记录以及更新结存数据
            productSerialService.deleteSerial(corpid, returnedPurchaseIdIn, XbbRefTypeEnum.RETURNED_PURCHASE.getCode());
            returnedPurchaseProductModel.deleteByReturnPurchaseIdIn(returnedPurchaseIdIn, corpid);

        } catch (Exception e) {
            LOG.error("ReturnedPurchaseProductServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return returnedPurchaseProductDeleteBatchVO;
    }
}
